import BaseEvent from '@/components/events/BaseEvent'
import { GENERAL_TYPES_MAP } from '@/common/const-general'
import { isHasOwnProperty, getCompIdByParam } from '@/plugins/utils'
import { CONTROL_TYPES } from '@/common/const-wb'
import { EVENT_HOVER_TYPES } from '@/common/events'
export default class GeneralEvent extends BaseEvent{
  constructor() {
    super();
  }

  excute({item, event, params, eventResultDisplay}){
    this.item = item;
    this.event = event;

    let url_ = '/feact/general';
    let isContinue_ = true;
    let controlId = event.controlId;
    let controlData = this.getItemControls[controlId];
    let ele_key = controlData.data.ele_key;

    params.ele_key = ele_key;
    const relateCompId = getCompIdByParam(controlData.data.relateCompId);
    const relateCompData = this.componentMap[relateCompId];
    if(relateCompData) {
      params.com_id = relateCompId;
    }
    if(ele_key == GENERAL_TYPES_MAP.gaobai) {
      isContinue_ = this.generalGaobai(controlData.data)
    }else if(ele_key == GENERAL_TYPES_MAP.area) {
      // delete params.com_id;
      if(!this.formValues[relateCompId]){
        this.showToast("亲，请选择一个选项呗！！！");
        isContinue_ = false
      }else{
        if(relateCompId && this.formValues[relateCompId]){
          params.area_code = this.formValues[relateCompId].value || this.formValues[relateCompId].item.value;
          //https://apib.hd.xxx.com/v1/feact/area_bind?site_id=&area_code=
          url_ = '/feact/area_bind'
        }else{
          url_ = ''
        }
      }
    }else if(ele_key == GENERAL_TYPES_MAP.draw){
      //http://apib.hd.xxx.com/v1/feact/general?site_id=1229&com_id=&ele_key=draw&ext_1={}
      let cardMap = null;
      let generalDataControlId = CONTROL_TYPES.wb_judge_right_wrong;//获取通用接口 controlId
      //在关联组件上点击领取卡片 1372 触发通用事件和获取通用事件不在一个组件上，通过关联组件将获取通用的组件id传到后端
      cardMap = _.cloneDeep(this.controls[params.com_id][generalDataControlId].data.cardMap);
      if(relateCompData && relateCompData.interactionData && relateCompData.interactionData.cardMap){
        //3d旋转组件给这个组件设置的值rotateIndex
        let listIndex = parseInt(this.item.commonAttr.itemIndex);
        if(listIndex != -1){
          const listKey = listIndex + 1;
          if(isHasOwnProperty(cardMap, listKey)){
            cardMap[listKey] += 1;
          }
        }
        //领取卡片，更新controls里存储的值
        this.store.commit('updateDraw', { id: params.com_id, controlId: generalDataControlId, type: params.ele_key, data: { ext_1: cardMap } })
      }else {
        //自身有自定义数据，每条数据都减1，合成事件
        let total = 0;
        let length = 0;
        _.forEach(cardMap, (num) => {
          length++;
          if(num > 0){
            total += 1;
          }
        })
        if(total >= length){
          _.forEach(cardMap, (num, key) => {
            cardMap[key] -= 1;
          })
        }
        this.store.commit('updateDraw', { id: params.com_id, controlId: generalDataControlId, type: params.ele_key, data: { ext_1: cardMap } })
      }
      params.ext_1 = JSON.stringify(cardMap);
    }else if(ele_key == GENERAL_TYPES_MAP.puzzle){
      //http://apib.hd.xxx.com/v1/feact/general?site_id=1229&com_id=&ele_key=puzzle&ext_1=
      let generalDataControlId = CONTROL_TYPES.wb_judge_right_wrong;//获取通用接口 controlId
      let puzzle = _.cloneDeep(this.interactionData.puzzle);
      let ext_1 = puzzle.join(',');
      if(relateCompData) {
        //配对 减一
        let matchLength = relateCompData.interactionData.puzzleMap.matchLength;
        let team_one = _.dropRight(puzzle, matchLength);
        let team_two = _.drop(puzzle, matchLength);
        let isSuccess = false;
        for(let i = 0, len = team_one.length; i < len; i++) {
          if(parseInt(team_one[i]) == 1 && parseInt(team_one[i]) == parseInt(team_two[i])) {
            team_one[i] = 2;
            team_two[i] = 2;
            isSuccess = true;
            break;
          }
        }
        puzzle = _.concat(team_one, team_two);
        ext_1 = puzzle.join(',');
        eventResultDisplay({ isSuccess, item: relateCompData, eventAttr: relateCompData.eventAttr, type: event.type, e: null, data: { ext_1 }, mouseBehavior:'' })
      }
      this.store.commit('updatePuzzle', { id: params.com_id, controlId: generalDataControlId, type: params.ele_key, data: { ext_1 } });
      params.ext_1 = ext_1;
    }else if(ele_key === GENERAL_TYPES_MAP.baoming){
      // 获取单位的值 站点2422 元气植物
      if(!this.formValues[relateCompId]) {
        this.showToast("亲，请选择一个选项呗！！！");
        isContinue_ = false
      } else {
        // 获取到对应的id值
        let index  = this.formValues[relateCompId].value || this.formValues[relateCompId].item.value;
        const relateEventCompId = getCompIdByParam(this.item.commonAttr.relateEventCompId);
        let relateEventCompData = this.componentMap[relateEventCompId]
        let obj = JSON.parse(relateEventCompData.events[EVENT_HOVER_TYPES.anchor.type].comps[2].attrs.value)
        if(obj){
          let pageId = obj.pages[index]
          params.ext_1 = pageId
        }
      }
    }
    return { url_, isContinue_ }
  }

  generalGaobai(data) {
    this.replaceComId(data, 'ext_1', 'uid')
    this.replaceComId(data, 'ext_4')
    this.replaceComId(data, 'ext_5')
    if (!data.ext_1 || data.ext_1 && data.ext_1.length <= 0) {
      this.showToast("还没有选择好友")
      return false
    }
    if (!data.ext_5 || data.ext_5 && data.ext_5.length <= 0) {
      this.showToast("还没有选择礼物")
      return false
    }
    return true
  }
  replaceComId(obj, key, matchKey) {
    let relateCompId = getCompIdByParam(obj[key])
    if (relateCompId && relateCompId.length > 1) {
      if (this.formValues[relateCompId]) {
        if (matchKey == 'uid') {
          obj[key] = this.formValues[relateCompId].item.uid ? this.formValues[relateCompId].item.uid : this.formValues[relateCompId].item.id
        } else {
          obj[key] = this.formValues[relateCompId].value
        }
      } else {
        obj[key] = ''
      }
    }
  }
}
